﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;

namespace WebSite_CodeMan.Sys_CodeMan_Manage
{
    public partial class Admin_Note_Mes : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            BaseClass.BaseClass.CheckState();
            if (!IsPostBack)
            {
                BaseClass.BaseClass.GetNoteClassMes(DDLClass);
                LoadData();
            }
        }

        private void LoadData()
        {
            int PageCount = 0;
            int RecordCount = 0;
            RpProduct.DataSource = BLL.NoteInfo.GetAllNoteInfo(PagerNote.CurrentPageIndex, PagerNote.PageSize, out PageCount, out RecordCount,GetFilter());
            RpProduct.DataBind();
            PagerNote.RecordCount = RecordCount;
        }

        /// <summary>
        /// 获取查询条件
        /// </summary>
        /// <returns></returns>
        private string  GetFilter()
        {
            string strFilter = " WHERE T_NoteClassID<>'' ";

            if (DDLClass.SelectedValue!="0")
            {
                strFilter += string.Format(" AND T_NoteClassID IN ('{0}')  ",BLL.NoteInfo.GetAboutClassMes(DDLClass.SelectedValue).Replace(",","','").Replace("'',","").Replace(",''",""));
            }
            else if (!string.IsNullOrEmpty(txtSearch.Value))
            {
                strFilter += string.Format(" AND (T_NoteTitle like '%{0}%'  or T_NoteDes  like '%{0}%' or T_NoteKey  like '%{0}%') ", txtSearch.Value.Trim());
            }
            return strFilter;
        }

        protected void BtnSeach_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        protected void BtnAllDel_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(HSelectID.Value.Trim()))
            {
                if (BLL.NoteInfo.DelNoteMes(HSelectID.Value.Trim()))
                {
                    // 删除相关图片、文件信息
                    string[] str = HImageID.Value.Split(',');
                    for (int i = 0; i < str.Length; i++)
                    {
                        if (!string.IsNullOrEmpty(str[i]))
                        {
                            Common.FileComm.DeleteFile(Server.MapPath("~/UpLoad/Images/") + str[i]);
                            Common.FileComm.DeleteFile(Server.MapPath("~/UpLoad/Images/Small/") + str[i]);
                        }
                    }

                    //删除静态页面文件
                    OleDbDataReader DataRead = BLL.NoteInfo.GetNoteClassENName(HClassID.Value.Trim());
                    if (DataRead.HasRows)
                    {
                        while (DataRead.Read())
                        {
                            string[] strNID = HSelectID.Value.Split(',');
                            for (int i = 0; i < strNID.Length; i++)
                            {
                                Common.FileComm.DeleteFile(Server.MapPath("~/"+DataRead["T_ClassEnName"].ToString()+"/") + strNID[i]+".html");
                            }
                        }
                    }
                    DataRead.Close();
                    DataRead.Dispose();
                    Common.MessageBox.Show(Page, "指定信息成功删除..", "Admin_Note_Mes.aspx", "suess");
                }
                else
                {
                    Common.MessageBox.Show(Page, "指定信息删除失败，请重试或联系技术人员..", "error");
                }
            }
        }
    }
}
